using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;
using InfoJetSoft.Service.Worker;

namespace InfoJetSoft.Service.Util
{
	/// <summary>
	/// Summary description for ExtGetDOMSelector.
	/// </summary>
	public class ExtGetDOMSelector
	{
        //CID 0927 //2011.06.21 //Begin
        public static List<XmlNode> selectNodes(Form form, XmlNode node, string filter, XmlNamespaceManager nsManager)
		{
            List<XmlNode> nodes = null;
            if (form.Manifest.EnableBindingTest)
            {
                nodes = new List<XmlNode>();
                XPathNodeIterator nodeIterator = XPathEvaluator.evaluateNodeList(form, node, filter);
                while (nodeIterator.MoveNext())
                {
                    XmlNode xmlNode = ((IHasXmlNode)nodeIterator.Current).GetNode();
                    nodes.Add(xmlNode);
                }
            }
            else  
            {
                XmlNodeList xmlNodeList = selectXmlNodeList(form, node, filter, nsManager);
                if (xmlNodeList != null)
                {
                    nodes = new List<XmlNode>();
                    foreach (XmlNode xmlNode in xmlNodeList)
                    {
                        nodes.Add(xmlNode);
                    }
                }
            }
            return nodes;
		}
        //CID 0927 //2011.06.21 //End
        
        private static XmlNodeList selectXmlNodeList(Form form, XmlNode node, string filter, XmlNamespaceManager nsManager)
        {
            if (filter.StartsWith("xdXDocument:GetDOM("))
            {
                int start = filter.IndexOf("xdXDocument:GetDOM(");
                int end = filter.IndexOf(")");
                string dataObjectName = filter.Substring(19 + 1, end - 19 - 2);
                string dataFilter = filter.Substring(end + 1);
                XmlDocument dataDoc = FormDataLoadWorker.GetXmlDoc(form, dataObjectName);
                if (dataDoc != null)
                {
                    NamespaceResolver dataNsResolver = new NamespaceResolver(dataDoc);
                    //CID 0418 //2009.10.08 //Begin
                    dataNsResolver.unifyNamespaces(form.Manifest);
                    //CID 0418 //2009.10.08 //End
                    return DOMUtil.selectNodes(dataDoc.DocumentElement, dataFilter, 0, dataNsResolver);
                }
                return null;
            }
            //CID 0230 //2009.03.06 //Begin
            else if (filter.StartsWith("xdXDocument:getDOM()"))
            {
                filter = filter.Replace("xdXDocument:getDOM()", "");
                return DOMUtil.selectNodes(form.XmlDoc, filter, 0, nsManager);
            }
            //CID 0230 //2009.03.06 //End
            else
            {
                //CID 0755 //2011.01.06 //Begin
                if (node.OwnerDocument != null && node.OwnerDocument != form.XmlDoc)
                {
                    XmlDocument dataDoc = node.OwnerDocument;
                    NamespaceResolver dataNsResolver = new NamespaceResolver(dataDoc);
                    dataNsResolver.unifyNamespaces(form.Manifest);
                    return DOMUtil.selectNodes(node, filter, 0, dataNsResolver);
                }
                else
                {
                    return DOMUtil.selectNodes(node, filter, 0, nsManager);
                }
                //CID 0755 //2011.01.06 //End
            }
        }

	}
}
